package com.BahavioralPartterns.strategyPattern.context;

import java.math.BigDecimal;

import com.BahavioralPartterns.strategyPattern.strategy.IStrategy;
import com.BahavioralPartterns.strategyPattern.strategy.PromotionalStrategy;
import com.BahavioralPartterns.strategyPattern.strategy.RebateStrategy;
import com.BahavioralPartterns.strategyPattern.strategy.ReduceStrategy;

/**
 * 上下文环境
 * 
 * @author
 * 
 */
public class Context {
	// 当前策略
	private IStrategy strategy;

	// 设置当前策略
	public void setStrategy(IStrategy strategy) {
		this.strategy = strategy;
	}

	public void switchStrategy(int x){
		switch (x) {
		case 0:
			// 打八折商品
			this.setStrategy(new RebateStrategy());
			break;
		case 1:
			// 满200，高于200部分打八折 商品
			this.setStrategy(new PromotionalStrategy());
			break;
		case 2:
			// 满1000减200 商品
			this.setStrategy(new ReduceStrategy());
			break;
		}
	}
	
	// 使用策略计算价格
	public double cul(double consumePrice) {
		// 使用具体商品促销策略获得实际消费金额
		double realPrice = this.strategy.realPrice(consumePrice);
		// 格式化保留小数点后1位，即：精确到角
		BigDecimal bd = new BigDecimal(realPrice);
		bd = bd.setScale(1, BigDecimal.ROUND_DOWN);
		return bd.doubleValue();
	}
}
